using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Tino.Controls
{
    public partial class WelcomePanel : UserControl
    {
        public WelcomePanel()
        {
            InitializeComponent();

            if (Globals.Get().LastOpenedFile != "" && File.Exists(Globals.Get().LastOpenedFile))
            {
                Button b = getButton(Globals.Get().LastOpenedFile);
                mainPanel.Controls.Add(b);
                b.Location = new Point(0, 70);
            }
        }

        Button getButton(string path)
        {
            FileInfo f = new FileInfo(path);

            Button b = new Button();
            b.Text = f.Name;
            b.Tag = path;
            b.BackColor = Color.LightSteelBlue;
            b.FlatStyle = FlatStyle.Flat;
            b.UseVisualStyleBackColor = false;
            b.Font = new Font("Tahoma", 9, FontStyle.Regular, GraphicsUnit.Point);
            FlatButtonAppearance fa = b.FlatAppearance;
            fa.BorderColor = mainPanel.BackColor;
            fa.BorderSize = 0;
            fa.MouseDownBackColor = Color.Transparent;
            fa.MouseOverBackColor = Color.Transparent;

            b.MouseHover += buttons_MouseOver;
            b.MouseLeave += buttons_MouseLeave;

            b.Click += openDbButton_Click;

            return b;
        }

        void buttons_MouseOver(object sender, EventArgs e)
        {
            ((Button)sender).ForeColor = Color.Red;
        }

        void buttons_MouseLeave(object sender, EventArgs e)
        {
            ((Button)sender).ForeColor = Color.Black;
        }

        void openDbButton_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (b.Tag == null)
                ((MainForm)Parent).Open();
            else
                ((MainForm)Parent).Open((string)b.Tag);
        }

        void createDbButton_Click(object sender, EventArgs e)
        {
            ((MainForm)Parent).New();
        }
    }
}
